﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fireball.Syntax.Parser;
using Fireball.Syntax;
using System.Text.RegularExpressions;

namespace Fireball.SyntaxParsers
{
    public class Parser
    {
        private Language _Language;
        private SyntaxDocument _SyntaxDocument;
        private Regex _LineSplitter = new Regex(@"\r\n|\n", RegexOptions.Multiline | RegexOptions.Compiled);

        public SyntaxDocument Document
        {
            get
            {
                return _SyntaxDocument;
            }
            set
            {
                _SyntaxDocument = value;
            }
        }

        public Language Language
        {
            get
            {
                return _Language;
            }
        }

        public Parser(Language language)
        {
            _Language = language;
        }

        public void ParseAll()
        {
            string text = _SyntaxDocument.Text;

            string[] lines = _LineSplitter.Split(text);

            _SyntaxDocument.Lines.Clear();

            foreach (string current in lines)
            {
                Line newLine = new Line()
                {
                    Text = current
                };

                _SyntaxDocument.Lines.Add(newLine);

                ParseLine(newLine);
            }
        }

        private void ParseLine(Line line)
        {
            string t = line.Text;
            if (string.IsNullOrEmpty(t))
                return;

            string[] words = t.Split(new string[] { " " }, StringSplitOptions.None);

            for(int i = 0;i < words.Length;i++)
            {
                string c = words[i];

                if (c == "")
                {
                    line.Add(new Space());
                    continue;
                }
                bool match = false;

                foreach (Pattern p in this.Language.Patterns)
                {
                    if (p.Match(c))
                    {
                        if (p.LineEntityType == "Keyword")
                            line.Add(new Word() { Text = c, IsKeyword = true, Style = p.Style });
                        else if (p.LineEntityType == "Operator")
                            line.Add(new Operator() { Text = c, Style = p.Style });
                        else if (p.LineEntityType == "Word")
                            line.Add(new Word() { Text = c, IsKeyword = false, Style = p.Style });
                        else
                            continue;

                        match = true;
                        break;
                    }
                }
                if (match == false)
                    line.Add(new Word() { Text = c });
                if (i < words.Length - 1)
                    line.Add(new Space());

            
            }
        }

    }
}
